package com.javacoo.xservice.base.support.hystrix;

import java.util.function.Function;

import com.netflix.hystrix.HystrixCommand;

import lombok.extern.slf4j.Slf4j;

/**
 * 调用超时Command
 * <p>说明:</p>
 * <li></li>
 * @author DuanYong
 * @param <P> 参数
 * @since 2017年11月13日下午2:24:51
 */
@Slf4j
public class InvokeTimeoutParamHystrixCommand<P>  extends HystrixCommand<Object>{
	/**正常处理函数*/
	private Function<P,Object> function;
	/**服务降级处理函数*/
	private Function<P,Object> fallbackFunction;
	/**参数*/
	private P p;
	/**方法名*/
	private String method;
	/**交易流水号*/
	private String transSn;
	/**超时时间*/
	private int timeout;

    public InvokeTimeoutParamHystrixCommand(Setter setter, Function<P,Object> function,Function<P,Object> fallbackFunction,P p,String method,String transSn,int timeout) {
        super(setter);
        this.function = function;
        this.fallbackFunction = fallbackFunction;
        this.p = p;
        this.method = method;
        this.transSn = transSn;
        this.timeout = timeout;
    }

	@Override
	protected Object run() throws Exception {
		return function.apply(p);
	}
	@Override
    public Object getFallback() {
		log.error("服务->{},请求超时,降级处理,服务调用超时时间:{}毫秒,流水号:{}",method,timeout,transSn);
		return fallbackFunction.apply(p);
    }

}
